package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.edu.koTA07.bean.StatusKunjunganBean;
import com.edu.koTA07.sqlHelper.SQLHelper;

public class StatusKunjunganUtils {
	SQLHelper helper;
	SQLiteDatabase database;	
	
	public StatusKunjunganUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
		
	@SuppressWarnings("static-access")
	public ArrayList<StatusKunjunganBean> loadData() {
		ArrayList<StatusKunjunganBean> currentData = new ArrayList<StatusKunjunganBean>();
		Cursor dataCursor = database.query(helper.TABLE_STATUS_KUNJUNGAN, 
				new String[] {helper.ID_STATUS_KUNJUNGAN, helper.ID_CUSTOMER1, helper.ID_PESANAN2, 
				helper.STATUS, helper.TGL_STATUS, helper.NAMA_PENGHANDLE}, 
				null, null, null, null,
				String.format("%s",  helper.ID_STATUS_KUNJUNGAN));
		
		dataCursor.moveToFirst(); StatusKunjunganBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				//GregorianCalendar date = new GregorianCalendar();
				String idStatusKunjungan= dataCursor.getString(0);
				String idCustomer= dataCursor.getString(1);
				String idPesanan = dataCursor.getString(2); 
				boolean status = dataCursor.getWantsAllOnMoveCalls();
				String tglStatus = dataCursor.getString(4);
				String namaPenghandle = dataCursor.getString(5);
				
				t = new StatusKunjunganBean(); 
				t.setIDStatusKunjungan(idStatusKunjungan);
				t.setIDCustomer(idCustomer);
				t.setIDPesanan(idPesanan); 
				t.setStatus(status);
				t.setTglStatus(tglStatus);
				t.setNamaPenghandle(namaPenghandle);
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}

	@SuppressWarnings("static-access")
	public void onSaveData(StatusKunjunganBean statusKunjungan) {
    	assert (null != statusKunjungan);
    	
    	ContentValues values = new ContentValues();
		values.put(helper.ID_CUSTOMER1, statusKunjungan.getIDCustomer());
		values.put(helper.ID_PESANAN2, Integer.parseInt(statusKunjungan.getIDPesanan()));
		values.put(helper.STATUS, statusKunjungan.getStatus());
		values.put(helper.TGL_STATUS, statusKunjungan.getTglStatus());
		values.put(helper.NAMA_PENGHANDLE, statusKunjungan.getNamaPenghandle());
		
		statusKunjungan.setIDStatusKunjungan(""+database.insert(helper.TABLE_STATUS_KUNJUNGAN, null, values));
    }
	
	@SuppressWarnings("static-access")
	public void onUpdateData(StatusKunjunganBean statusKunjungan) {
    	assert (null != statusKunjungan);    	
    	ContentValues values = new ContentValues();
		values.put(helper.ID_PESANAN2, Integer.parseInt(statusKunjungan.getIDPesanan()));
				
		String idStatusKunjungan = statusKunjungan.getIDStatusKunjungan();
		String where = String.format("%s = %s", helper.ID_STATUS_KUNJUNGAN, idStatusKunjungan);
		database.update(helper.TABLE_STATUS_KUNJUNGAN, values, where, null); 
    }
}
